package org.abacus.helper;

public class StarHelper {
	public StarHelper(Long[] data) {
		super();
		done(data);
	}

	private double max = 0.0;
	private double min = 0.0;

	private int maxStar = 5;
	private int minStar = 1;

	public int getStars(Long c) {
		if (null == c) {
			return minStar;
		}
		if (minStar == maxStar) {
			return maxStar;
		}
		if (max == c.doubleValue()) {
			return maxStar;
		}
		if (max == min && min == c.doubleValue()) {
			return maxStar;
		}
		if (min == c.doubleValue()) {
			return minStar;
		}

		double k = (maxStar - minStar) / (max - min);

		double b = maxStar - k * max;

		double x0 = c.doubleValue();
		double x = 0;
		for (int y = minStar + 1; y < maxStar; y++) {
			x = (y - b) / k;
			if (x0 < x) {
				return y;
			}
		}
		return maxStar;
	}

	private void done(Long[] data) {
		if (null != data && data.length > 0) {
			int i = data.length - 1;
			if (data[i] == null) {
				max = data[i].doubleValue();
			}
			if (i == 0) {
				return;
			}
			i = 0;
			if (data[i] != null) {
				min = data[i].doubleValue();
			}
			for (i = 1; i < data.length - 1; i++) {
				if (data[i] != null) {
					if (data[i] > max) {
						max = data[i].doubleValue();
					}
					if (data[i] < min) {
						min = data[i].doubleValue();
					}
				}
			}
		}
	}

	public int getMaxStar() {
		return maxStar;
	}

	public void setMaxStar(int maxStar) {
		this.maxStar = maxStar;
	}

	public int getMinStar() {
		return minStar;
	}

	public void setMinStar(int minStar) {
		this.minStar = minStar;
	}
}
